package com.common;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SessionFilter implements Filter {

	FilterConfig config;
	
	public void destroy() {
		

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
		 	  request.setCharacterEncoding("UTF-8");
			  HttpServletRequest req=(HttpServletRequest)request;
			  HttpServletResponse res=(HttpServletResponse )response;
			  HttpSession session=req.getSession(true);
			  String requesturi = req.getRequestURI();
			  if(session.getAttribute("user_id")==null
					  || "".equals(session.getAttribute("user_id").toString().trim()) 
					  ){
				  
					if(requesturi.endsWith("/login.jsp")||requesturi.endsWith("/login.js"))
					{
						chain.doFilter(request, response);						
						 
					}else if(requesturi.endsWith(".html")||requesturi.endsWith(".js")
							||requesturi.endsWith(".php")||requesturi.endsWith(".jsp")
							||requesturi.endsWith(".asp")||requesturi.endsWith(".aspx")
							){
							String url=req.getContextPath();
						  res.sendRedirect(url+"/login/login.jsp");
					}else{
						 chain.doFilter(request, response);
					}				
				  
			  }else{	
				 // res.sendRedirect("/fjjlshy/admin/login.html");
				  chain.doFilter(request, response);	   
				  
			  }


	}

	public void init(FilterConfig config) throws ServletException {
		
	}

}
